<template>
  <div class="app-container">
    <!-- 优惠券领取查询模块 -->
    <div class="receivableSheet">
      <!-- 查询模块 -->
      <el-form ref="form" :model="form" label-width="130px" :inline="true">
        <el-row>
          <el-col :span="6">
            <el-form-item label="优惠券名称">
              <el-input v-model="form.ticket_name" />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="会员姓名">
              <el-input v-model="form.username" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="会员手机号">
              <el-input v-model="form.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="领取时间" width="236">
              <el-date-picker
                v-model="form.get_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="过期时间" width="236">
              <el-date-picker
                v-model="form.end_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="消费时间" width="236">
              <el-date-picker
                v-model="form.consumption_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="form.status" placeholder="请选择">
                <el-option
                  v-for="(item,index) in statusType"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="2">
            <!-- <el-form-item> -->
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <!-- pointer-events:none -->
            <!-- <el-button
                            type="primary"
                            @click="handleExport"
                            v-loading="tableLoading1"
                            element-loading-text="拼命加载中"
                            element-loading-spinner="el-icon-loading"
                            :class="isShow?'isHide':''"
            >导出</el-button>-->
            <!-- </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <el-table-column fixed type="index" label="编号" width="50" align="center" />
        <el-table-column prop="ticket_name" label="优惠券名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.ticket_name.ticket_name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ticket_type" label="优惠券类型" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.ticket_name.ticket_type }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="realname" label="会员姓名" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.member.realname }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="手机号" width="100" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.username | phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="领取时间" align="center" />
        <el-table-column prop="end_time" label="过期时间" align="center" />
        <el-table-column prop="updated_at" label="消费时间" align="center" />
        <el-table-column prop="consumption" label="消费门店" align="center" />
        <el-table-column prop="source" label="来源" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.ticket_name.source }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="is_use" label="状态" align="center" />
      </el-table>
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { coupon, couponExport } from "@/api/plusMange";
import commonFn from "@/utils/common";
export default {
  name: "CouponsList",
  data() {
    return {
      form: {
        ticket_name: "",
        username: "",
        phone: "",
        get_time: "",
        end_time: "",
        consumption_time: "",
        status: "",
        page: 1,
        page_size: 10
      },
      // 时间
      dates: null,
      tableLoading: true,
      tableLoading1: false, //导出加载
      isShow: false,
      tableData: [],
      // 状态类型
      statusType: [
        { name: "全部", value: "" },
        { name: "未使用", value: "1" },
        { name: "已使用", value: "2" },
        { name: "已过期", value: "3" }
      ],
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 60 // 总条目数,
    };
  },
  created() {
    this.getCoupon(this.form);
  },
  methods: {
    getCoupon(e) {
      coupon(e)
        .then(res => {
          // console.log(res);
          if (res.data.code == 200) {
            // console.log(res);
            const resData = res.data.data;
            this.tableData = resData.data;
            this.page_totals = resData.total;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          // console.log(err);
          this.errorStatus(err.msg);
          this.tableLoading = false;
        });
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      this.getCoupon(this.form);
    },
    // 导出
    handleExport() {
      this.tableLoading1 = true;
      this.isShow = true;
      const listExport = { ...this.form };
      delete listExport.page;
      delete listExport.page_size;
      couponExport(listExport)
        .then(res => {
          commonFn.downFile("优惠券领取导出", res);
          this.tableLoading1 = false;
          this.isShow = false;
        })
        .catch(err => {
          this.errorStatus(err.msg);
          this.tableLoading1 = false;
          this.isShow = false;
        });
    },
    // 每页显示条数
    handleSizeChange(val) {
      this.form.page_size = val;
      this.getCoupon(this.form);
    },
    // 当前页
    handleCurrentChange(val) {
      this.form.page = val;
      this.getCoupon(this.form);
    },
    // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 成功状态弹窗
    succStatus(txt, status) {
      this.eleMsg(txt, "success");
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    }
  }
};
</script>
<style scoped>
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
/* 分页样式 */
.pagesCon {
  margin-top: 20px;
  text-align: right;
}
/* 禁止点击 */
.isHide {
  pointer-events: none;
}
</style>
